#include <linux/init.h>
#include <linux/sched.h>
#include <linux/printk.h>
#include <linux/sched/task.h>
#include <linux/delay.h>
#include <msh/export.h>

#include <stdlib.h>

static float fadd(float a, float b, float c)
{
    return a * c + b * c;
}

static int fair_task0(void *arg)
{
    struct task_struct *self = current;
    float cnt = 0;
    float a = 0.1, b = 0.2;
    float c, d;

    while (1)
    {
        c = fadd(a, b, cnt);
        d = fadd(a, b, cnt);
        cnt++;
        if (c != d)
        {
            printk("%s %d\n", self->name, jiffies);
        }
    }

    return 0;
}

static int fair_task1(void *arg)
{
    struct task_struct *self = current;
    float cnt = 0;
    float a = 1.1, b = 2.2;
    float c, d;

    while (1)
    {
        c = fadd(a, b, cnt);
        d = fadd(a, b, cnt);
        cnt++;
        if (c != d)
        {
            printk("%s %d\n", self->name, jiffies);
        }
    }

    return 0;
}

static int fair(int argc, char **argv)
{
    int fa = 0;
    int a = 0;

    while (argc > 1)
    {
        if (argc == 2)
            fa = atoi(argv[1]);
        else if (argc == 3)
            a = atoi(argv[2]);

        argc--;
    }

    if (fa == 0)
    {
        kernel_thread(fair_task0, (void*)a, "fairT0", 0);
    }
    else if (fa == 1)
    {
        kernel_thread(fair_task1, (void*)a, "fairT1", 0);
    }

    return 0;
}
MSH_CMD_EXPORT(fair, test fair);
